package net.valerijtimakov.homefinances.dao.storages.common.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import net.valerijtimakov.homefinances.dao.storages.finances.mysql.IMySqlProvider;
import net.valerijtimakov.homefinances.domain.common.DataItem;
import net.valerijtimakov.homefinances.domain.common.OwnedNamedtem;
import net.valerijtimakov.homefinances.exceptions.AppExceptionWrongState;

public abstract class OwnedNamedItemsStorage extends OwnedItemsStorage {
	
	public OwnedNamedItemsStorage(IMySqlProvider provider) throws AppExceptionWrongState {
		super(provider);
	}

	protected String nameFieldName = "name";
	protected String descriptionFieldName = "description";

	@Override
	protected <T extends DataItem> void parse(ResultSet rs, T item) throws SQLException {
		super.parse(rs, item);
		OwnedNamedtem cItem = (OwnedNamedtem)item;
		cItem.setName(rs.getString(nameFieldName));
		cItem.setDescription(rs.getString(descriptionFieldName));
	}
	
	@Override
	protected void initSelect() {
		super.initSelect();
		selectQuery.addField(nameFieldName);
		selectQuery.addField(descriptionFieldName);
	}

	@Override
	protected <T extends DataItem> void addData(T item) {
		super.addData(item);
		OwnedNamedtem cItem = (OwnedNamedtem)item;
		insertQuery.setFieldValue(nameFieldName, cItem.getName());
		insertQuery.setFieldValue(descriptionFieldName, cItem.getDescription());
	}

	@Override
	protected <T extends DataItem> void setUpdatedFields(T item) {
		super.setUpdatedFields(item);
		OwnedNamedtem cItem = (OwnedNamedtem)item;
		updateQuery.setFieldValue(descriptionFieldName, cItem.getDescription());
		updateQuery.setFieldValue(nameFieldName, cItem.getName());
	}

}
